package com.cskaoyan.gateway.controller.user;

import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.commons.tool.utils.CookieUtil;
import com.mall.user.IKaptchaService;
import com.mall.user.IRegisterService;
import com.mall.user.ISendMailService;
import com.mall.user.annotation.Anoymous;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.dto.KaptchaCodeRequest;
import com.mall.user.dto.KaptchaCodeResponse;
import com.mall.user.dto.UserRegisterRequest;
import com.mall.user.dto.UserRegisterResponse;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.net.SocketTimeoutException;
import java.util.Map;

/**
 *  注册controller
 *
 */
@RestController
@RequestMapping("/user")
public class RegisterController {

    @Reference(timeout = 3000,retries = 0)
    IKaptchaService iKaptchaService;

    @Reference(timeout = 3000,retries = 0,check = false)
    IRegisterService registerService;

    // 发送邮件的service
//    @Reference(timeout = 3000,retries = 0,check = false)
//    ISendMailService sendMailService;


//    @RequestMapping("register")
//    @Anoymous
//    public ResponseData userRegister(UserRegisterRequest registerRequest, HttpServletRequest request){
//        // 注册码controller已经实现了，所以不需要我们去关心
//        ResponseUtil<Object> responseUtil = new ResponseUtil<>();
//        // 判断验证码是否校验成功
//        // 如果setErrorMsg方法没有执行，数据为空，uuid有值，就代表通过了验证码校验
//        String uuid = CookieUtil.getCookieValue(request, "kaptcha_uuid");
//        UserRegisterResponse registerResponse = null;
//        if(uuid != null){
//            // 校验成功
//            registerResponse = registerService.addRegisterMember(registerRequest,uuid);
//            // 发送邮件 -->  调用sendMailService中的sendMail方法
//            sendMailService.sendMail();
//            return responseUtil.setData(registerResponse);
//        }
//        // 验证码校验失败
//        return responseUtil.setErrorMsg(registerResponse.getMsg());
//    }

    @PostMapping("register")
    @Anoymous
    public ResponseData register(@RequestBody Map<String,String> map,HttpServletRequest request){
        // 使用map的形式接收参数
        String userName = map.get("userName");
        String userPwd = map.get("userPwd");
        String captcha = map.get("captcha");
        String email = map.get("email");

        // 验证验证码
        KaptchaCodeRequest kaptchaCodeRequest = new KaptchaCodeRequest();
        String uuid = CookieUtil.getCookieValue(request,"kaptcha_uuid");

        kaptchaCodeRequest.setUuid(uuid);
        kaptchaCodeRequest.setCode(captcha);
        KaptchaCodeResponse response = iKaptchaService.validateKaptchaCode(kaptchaCodeRequest);
        String code = response.getCode();
        if(!code.equals(SysRetCodeConstants.SUCCESS.getCode())){
            // 验证码校验错误
            return new ResponseUtil<>().setErrorMsg(response.getMsg());
        }
        // 校验成功，去向用户表中插入记录
        UserRegisterRequest registerRequest = new UserRegisterRequest();
        registerRequest.setEmail(email);
        registerRequest.setUserName(userName);
        registerRequest.setUserPwd(userPwd);
        UserRegisterResponse registerResponse = registerService.register(registerRequest);
        // 根据返回结果中的code判断是否成功还是失败
        if(registerResponse.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())){
            // 数据添加成功，返回的响应数据可以为null
            return new ResponseUtil<>().setData(null);
        }
        // 数据添加失败
        return  new ResponseUtil<>().setErrorMsg(registerResponse.getMsg());
    }

}
